$(function(){
	//构建答案
	queryAnswer();
	//获取用户提交的附件
	queryAnswerFiles();
	//获取答卷主信息
	querDiagnosis();
});	


/**
 * 构建答案
 */
function queryAnswer()
{
	var questionnaireId=$("#questionnaireId").val();
	var questionnaireAnswerId=$("#questionnaireAnswerId").val();
	$("div[name^='question_']").each(function(obj){
		var questionId=$(this).attr("title");//问题ID
		var questionType=$(this).attr("type");//题型，1：单选，2：多选，3：问答
		$.ajax({
			type: "post",
			url: "/pac/questionnaire/question/answer/get",
			data:{
				questionnaireId:questionnaireId,
				questionId:questionId,
				answerId:questionnaireAnswerId
			},
			success: function(data){
				var answers=data.answer;
				if(data.result==true&&null!=answers&&answers.length>0)
				{
					//单选
					if(questionType=='1')
					{
						var answerObj=answers[0];
						$("#question_"+questionId+"_"+answerObj.optionId).css("text-decoration","underline");
						$("#question_"+questionId+"_"+answerObj.optionId).css("display","inline");
						$("#question_"+questionId+"_"+answerObj.optionId).attr("answerid",answerObj.id);
						$(".div_question_"+questionId+"_"+answerObj.optionId).css("display","inline");
					}
					//多选
					else if(questionType=='2')
					{
						for(var n=0;n<answers.length;n++)
						{
							var answerObj=answers[n];
							$("#question_"+questionId+"_"+answerObj.optionId).css("text-decoration","underline");
							$("#question_"+questionId+"_"+answerObj.optionId).css("display","inline");
							$("#question_"+questionId+"_"+answerObj.optionId).attr("answerid",answerObj.id);
							$(".div_question_"+questionId+"_"+answerObj.optionId).css("display","inline");
						}
					}
					//问答
					else if(questionType=='3')
					{
						var answerObj=answers[0];
						$("#question_"+questionId).css("text-decoration","underline");
						$("#question_"+questionId).html(answerObj.answer);
						$("#question_"+questionId).attr("answerid",answerObj.id);
					}
					
				}
			}
		});
	});
}

/**
 * 构建附件列表
 */
function queryAnswerFiles()
{
	var questionnaireAnswerId=$("#questionnaireAnswerId").val();
	$.ajax({
		type: "post",
		url: "/pac/questionnaire/answer/files",
		data:{
			answerId:questionnaireAnswerId
		},
		success: function(data){
			var answerFiles=data;
			if(null!=answerFiles&&answerFiles.length>0)
			{
				$("#div_answerFiles").append("<h3>检查结果：</h3>");
				for(var n=0;n<answerFiles.length;n++)
				{
					var answerFile=answerFiles[n];
					var strFiles="<a href="+answerFile.path+" target='blank'>附件"+(n+1)+"</a>&nbsp;&nbsp;";
					$("#div_answerFiles").append(strFiles);
				}
			}
		}
	});
}

//获取诊断结果
function querDiagnosis()
{
	var questionnaireAnswerId=$("#questionnaireAnswerId").val();
	$.ajax({
		type: "post",
		url: "/pac/questionnaire/diagnosis/get",
		data:{
			diagnosisId:questionnaireAnswerId
		},
		success: function(data){
			$("#spAnswerUser").html(data.patientName);
			$("#spAnswerDate").html(data.answerDate);
			if(""!=data.diagnosisDate&&null!=data.diagnosisDate)
			{
				$("#div_diagnosis_content").show();
				$("#td_diagnosis").html(data.diagnosis);
				$("#td_doctorName").html(data.doctorName);
				$("#td_diagnosis_date").html(data.diagnosisDate);
			}
			else
			{
				$("#div_diagnosis").show();
				$("#div_btn_diagnosis").css("display","inline");
				$("#div_btn_edit_answer").css("display","inline");
			}
		}
	});
}

//提交诊断
function submitDiagnosis()
{
	var questionnaireAnswerId=$("#id").val();
	
	$.ajax({
		type: "post",
		url: "/pac/questionnaire/answer/files",
		data:{
			answerId:questionnaireAnswerId
		},
		success: function(data){
			var answerFiles=data;
			if(null!=answerFiles&&answerFiles.length>0)
			{
				//其他流产方式
				var zdHandleOther=$("#txtZdHandle_other").prop("checked");
				if(true==zdHandleOther)
				{
					var zdHandleOtherCon=$("#txtZdHandle_other_content").textbox("getValue");
					if(null==zdHandleOtherCon||""==zdHandleOtherCon||undefined==zdHandleOtherCon)
					{
						$.messager.alert('警告','请填写其他选项值','error'); 
						return false;
					}
					$("#txtZdHandle_other").val(zdHandleOtherCon);
				}
				
				//提交诊断结果
				submitDiagnosisDo();
			}
			else
			{
				$.messager.alert('提示','需要患者上传检查结果后才能给出诊断！','warning'); 
			}
		}
	});
}

/**
 * 提交诊断结果
 */
function submitDiagnosisDo()
{
	$('#frmQuestionnaireDiagnosis').form('submit',{    
	    url:"/pac/questionnaire/diagnosis",    
	    onSubmit: function(){    
	    	return $(this).form('validate');
	    },    
	    success:function(jsonstr){    
	    	try
	    	{
	    		var data=JSON.parse(jsonstr);   
		    	if(data.result==true)
				{
		    		$("#div_diagnosis").hide();
					$("#div_btn_diagnosis").hide();
					$("#div_btn_edit_answer").hide();
					
					querDiagnosis();
				}
		    	else
		    	{
		    		$.messager.alert('提示',data.msg,'info');
		    	}
	    	}
	    	catch(e)
	    	{
	    		var msgAle="您的浏览器无法解析JSON，请<a href='/pac/images/plugin/IE自动解析JSON.zip' style='color: blue;'>下载插件</a>！";
	    		$.messager.alert('提示',msgAle,'info');
	    	}
	    	
	    }    
	});   
}